package com.linktrip.devicemanager.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.linktrip.devicemanager.entity.DeviceInfo;

import io.netty.channel.ChannelHandlerContext;

public class ToolKits {
	static Logger log = LoggerFactory.getLogger(ToolKits.class);
	static DevicesListHelper helper = DevicesListHelper.getSingleton();

	public static String getRequestParamValue(HttpServletRequest request, String param) {
		try {
			if (request.getParameter(param) == null || request.getParameter(param).trim().equals("")) {
				return null;
			}
			return request.getParameter(param);
		} catch (Exception e) {
			log.info(e.getMessage());
			return null;
		}
	}

	public static int getRequestParamValueForInt(HttpServletRequest request, String param) {
		try {
			if (request.getParameter(param) == null || request.getParameter(param).equals("")) {
				return -1;
			}
			return Integer.parseInt(request.getParameter(param));
		} catch (Exception e) {
			log.info(e.getMessage());
			return -1;
		}
	}

	public static long getRequestParamValueForLong(HttpServletRequest request, String param) {
		try {
			if (request.getParameter(param) == null) {
				return -1L;
			}
			return Long.parseLong(request.getParameter(param));
		} catch (Exception e) {
			log.info(e.getMessage());
			return -1L;
		}
	}

	public static int parse2Int(Object value) {
		try {
			if (value == null) {
				return 0;
			}
			return Integer.parseInt((String) value);
		} catch (Exception e) {
			log.info(e.getMessage());
			return 0;
		}
	}

	public static void removeDeviceFromList(String id) {
		log.info("id:{} socket remove from Map", id);
		helper.removeFromMap(id);
	}

	public static void putDeviceSocketIntoMap(String id, ChannelHandlerContext soc) {
		log.info("id:{} socket put into Map", id);
		helper.addIntoMap(id, soc);
	}

	public static Map<String, ChannelHandlerContext> getDeviceSocketsMap_nio() {
		return helper.getDevicesMap_nio();
	}

	public static void addMessageIntoMap(String id, Integer value) {
		log.info("id:{} value:{} message put into Map", id, value);
		helper.putMessage(id, value);
	}

	public static void removeMessageFromMap(String id) {
		log.info("id:{} message remove from Map", id);
		helper.removeMessage(id);
	}

	public static int getMessageByKey(String key) {
		return helper.getMessageByKey(key);
	}
	
	public static void addDeviceInfo(String key,DeviceInfo info){
		helper.putDeviceInfo(key, info);
	}
	
	public static DeviceInfo getDeviceInfo(String key){
		return helper.getDeviceInfo(key);
	}

	public static boolean isEmpty(String value) {
		if (null == value || "".equals(value.trim()))
			return true;

		return false;
	}

	public static long date2Long(String datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = null;
		try {
			d = sdf.parse(datetime);
		} catch (ParseException e) {
			return -1L;
		}
		return d.getTime();
	}

	public static String getMDHMS() {
		SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
		return sdf.format(new Date());
	}

	public static String getFullString(String value, int len) {
		StringBuilder tmp = new StringBuilder();
		for (int i = len - value.length(); i > 0; i--)
			tmp.append("0");
		return tmp.append(value).toString();
	}

	public static boolean hasValue(Integer... integers) {
		try {
			for (Integer i : integers) {
				if (null == i || 0 == i || -1 == i)
					return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
